package com.fa.wisdom.ui;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.SpUtils;
import com.fa.wisdom.utils.TitleBuilder;
import com.fa.wisdom.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;

public class AddHomeActivity extends BaseActivity {
    ImageView iv_title_right;
    EditText et_nickname;
    int homeID;
    @Override
    public void setListen() {
        homeID=getIntent().getIntExtra("homeID",0);
        et_nickname = findViewById(R.id.et_nickname);
        iv_title_right = findViewById(R.id.iv_title_right);
        iv_title_right.setImageResource(R.drawable.home_add);
        new TitleBuilder(this).setTitleText("添加房间")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                }).setRightIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addGroup();
            }
        });
    }
    private int pid;
    @Override
    public void setTitleBarColor() {
         pid = SpUtils.getInstance().decodeInt("group_id", 0);
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_add_home;
    }

    @Override
    public void initView() {

    }

    //添加分组
    private void addGroup() {
        String name = et_nickname.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showError("房间名字不能为空");
            return;
        }
        LoadingDialog dialog= Loading.getInstance().onShowLargeLoading("添加中...");
        dialog.showInActivity(this);
        String token = SpUtils.getInstance().decodeString(Constant.TOKEN, "");
        int userID = SpUtils.getInstance().decodeInt(Constant.User_id, -1);

        OkHttpUtils.post().url("https://br.feiaikeji.cn/index.php/api/device/device_group_add")
                .addParams("token", token)
                .addParams("user_id", String.valueOf(userID))
                .addParams("name", name)
                .addParams("pid", String.valueOf(pid))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.dismiss();
                ToastUtils.showSuccess("添加成功");
                EventBus.getDefault().postSticky("refresh");
                finish();
            }
        });
    }

    @Override
    public void initData() {

    }
}
